home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0045_Maze Generator.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  13KB  |  473 lines

  1. {
  2. randyd@csd4.csd.uwm.edu (Randall Elton Ding)
  3.  
  4. This is really for Allen who earlier in the month asked about generating
  5. a maze in pascal.  It may not really be the fastest, but I know of
  6. no other way which is faster.  Check it out, it lets you try to move
  7. thru the maze, when you give up it shows you the way.  It has variable
  8. difficulty and size too.
  9.  
  10. This was origionally written in Apple][ 6502 machine language, I ported
  11. it over to pascal a few years later.
  12. }
  13.  
  14. (* Big Mind Over Maze
  15.    maze generator and solver
  16.    created by Randy Ding
  17.    July 16,1983   <April 21,1992>  *)
  18.  
  19. {$R-}   { range checking }
  20.  
  21. program makemaze;
  22.  
  23. uses
  24.   crt, graph;
  25.  
  26. const
  27.   screenwidth   = 640;
  28.   screenheight  = 350;
  29.   minblockwidth = 2;
  30.   maxx = 200;   { [3 * maxx * maxy] must be less than 65520 (memory segment) }
  31.   maxy = 109;   { here maxx/maxy about equil to screenwidth/screenheight }
  32.   flistsize = 5000; { flist size (fnum max, about 1/3 of maxx * maxy) }
  33.  
  34.   background = black;
  35.   gridcolor  = green;
  36.   solvecolor = white;
  37.  
  38.   rightdir = $01;
  39.   updir    = $02;
  40.   leftdir  = $04;
  41.   downdir  = $08;
  42.  
  43.   unused   = $00;    { cell types used as flag bits }
  44.   frontier = $10;
  45.   reserved = $20;
  46.   tree     = $30;
  47.  
  48.  
  49. type
  50.   frec = record
  51.           column, row : byte;
  52.          end;
  53.   farr = array [1..flistsize] of frec;
  54.  
  55.   cellrec = record
  56.               point : word;  { pointer to flist record }
  57.               flags : byte;
  58.             end;
  59.   cellarr = array [1..maxx,1..maxy] of cellrec;
  60.  
  61.   {
  62.     one byte per cell, flag bits...
  63.  
  64.     0: right, 1 = barrier removed
  65.     1: top    "
  66.     2: left   "
  67.     3: bottom "
  68.     5,4: 0,0 = unused cell type
  69.          0,1 = frontier "
  70.          1,1 = tree     "
  71.          1,0 = reserved "
  72.     6: (not used)
  73.     7: solve path, 1 = this cell part of solve path
  74.   }
  75.  
  76.  
  77. var
  78.   flist     : farr;         { list of frontier cells in random order }
  79.   cell      : ^cellarr;      { pointers and flags, on heap }
  80.   fnum,
  81.   width,
  82.   height,
  83.   blockwidth,
  84.   halfblock,
  85.   maxrun    : word;
  86.   runset    : byte;
  87.   ch        : char;
  88.  
  89. procedure initbgi;
  90. var
  91.   grdriver,
  92.   grmode,
  93.   errcode : integer;
  94. begin
  95.   grdriver := DETECT;
  96.   grmode   := EGAhi;
  97.   initgraph(grdriver, grmode, 'e:\bp\bgi');
  98.   errcode:= graphresult;
  99.   if errcode <> grok then
  100.   begin
  101.     writeln('Graphics error: ', grapherrormsg(errcode));
  102.     halt(1);
  103.   end;
  104. end;
  105.  
  106.  
  107. function adjust(var x, y : word; d : byte) : boolean;
  108. begin                              { take x,y to next cell in direction d }
  109.   case d of                        { returns false if new x,y is off grid }
  110.     rightdir:
  111.     begin
  112.       inc (x);
  113.       adjust:= x <= width;
  114.     end;
  115.  
  116.     updir:
  117.     begin
  118.       dec (y);
  119.       adjust:= y > 0;
  120.     end;
  121.  
  122.     leftdir:
  123.     begin
  124.       dec (x);
  125.       adjust:= x > 0;
  126.     end;
  127.  
  128.     downdir:
  129.     begin
  130.       inc (y);
  131.       adjust:= y <= height;
  132.     end;
  133.   end;
  134. end;
  135.  
  136.  
  137. procedure remove(x, y : word);      { remove a frontier cell from flist }
  138. var
  139.   i : word; { done by moving last entry in flist into it's place }
  140. begin
  141.   i := cell^[x,y].point;          { old pointer }
  142.   with flist[fnum] do
  143.     cell^[column,row].point := i;   { move pointer }
  144.   flist[i] := flist[fnum];        { move data }
  145.   dec(fnum);                    { one less to worry about }
  146. end;
  147.  
  148.  
  149. procedure add(x, y : word; d : byte);  { add a frontier cell to flist }
  150. var
  151.   i : byte;
  152. begin
  153.   i := cell^[x,y].flags;
  154.   case i and $30 of   { check cell type }
  155.     unused :
  156.     begin
  157.       cell^[x,y].flags := i or frontier;  { change to frontier cell }
  158.       inc(fnum);                        { have one more to worry about }
  159.       if fnum > flistsize then
  160.       begin     { flist overflow error! }
  161.         dispose(cell);  { clean up memory }
  162.         closegraph;
  163.         writeln('flist overflow! - To correct, increase "flistsize"');
  164.         write('hit return to halt program ');
  165.         readln;
  166.         halt(1);        { exit program }
  167.       end;
  168.       with flist[fnum] do
  169.       begin    { copy data into last entry of flist }
  170.         column := x;
  171.         row    := y;
  172.       end;
  173.       cell^[x,y].point := fnum; { make the pointer point to the new cell }
  174.       runset := runset or d;   { indicate that a cell in direction d was }
  175.     end;                      {    added to the flist }
  176.  
  177.     frontier : runset := runset or d;     { allready in flist }
  178.   end;
  179. end;
  180.  
  181.  
  182. procedure addfront(x, y : word);    { change all unused cells around this }
  183. var                              {    base cell to frontier cells }
  184.   j, k : word;
  185.   d    : byte;
  186. begin
  187.   remove(x, y);       { first remove base cell from flist, it is now }
  188.   runset := 0;         {    part of the tree }
  189.   cell^[x,y].flags := cell^[x,y].flags or tree;   { change to tree cell }
  190.   d := $01;            { look in all four directions- $01,$02,$04,$08 }
  191.   while d <= $08 do
  192.   begin
  193.     j := x;
  194.     k := y;
  195.     if adjust(j, k, d) then
  196.       add(j, k, d);  { add only if still in bounds }
  197.     d := d shl 1;    { try next direction }
  198.   end;
  199. end;
  200.  
  201.  
  202. procedure remline(x, y : word; d : byte);  { erase line connecting two blocks }
  203. begin
  204.   setcolor(background);
  205.   x := (x - 1) * blockwidth;
  206.   y := (y - 1) * blockwidth;
  207.   case d of
  208.     rightdir : line (x + blockwidth, y + 1, x + blockwidth, y + blockwidth - 1);
  209.     updir    : line (x + 1, y, x + blockwidth - 1, y);
  210.     leftdir  : line (x, y + 1, x, y + blockwidth - 1);
  211.     downdir  : line (x + 1, y + blockwidth, x + blockwidth - 1, y + blockwidth);
  212.   end;
  213. end;
  214.  
  215.  
  216. { erase line and update flags to indicate the barrier has been removed }
  217. procedure rembar(x, y : word; d : byte);
  218. var
  219.   d2 : byte;
  220. begin
  221.   remline(x, y, d);       { erase line }
  222.   cell^[x,y].flags := cell^[x,y].flags or d; { show barrier removed dir. d }
  223.   d2 := d shl 2;  { shift left twice to reverse direction }
  224.   if d2 > $08 then
  225.     d2 := d2 shr 4;  { wrap around }
  226.   if adjust(x, y, d) then  { do again from adjacent cell back to base cell }
  227.     cell^[x,y].flags := cell^[x,y].flags or d2;    { skip if out of bounds }
  228. end;
  229.  
  230.  
  231. function randomdir : byte;  { get a random direction }
  232. begin
  233.   case random(4) of
  234.     0 : randomdir := rightdir;
  235.     1 : randomdir := updir;
  236.     2 : randomdir := leftdir;
  237.     3 : randomdir := downdir;
  238.   end;
  239. end;
  240.  
  241.  
  242. procedure connect(x, y : word);    { connect this new branch to the tree }
  243. var                             {    in a random direction }
  244.   j, k  : word;
  245.   d     : byte;
  246.   found : boolean;
  247. begin
  248.   found := false;
  249.   while not found do
  250.   begin { loop until we find a tree cell to connect to }
  251.     j := x;
  252.     k := y;
  253.     d := randomdir;
  254.     if adjust(j, k, d) then
  255.       found := cell^[j,k].flags and $30 = tree;
  256.   end;
  257.   rembar(x, y, d);   { remove barrier connecting the cells }
  258. end;
  259.  
  260.  
  261. procedure branch(x, y : word);  { make a new branch of the tree }
  262. var
  263.   runnum : word;
  264.   d      : byte;
  265.   i      : boolean;
  266. begin
  267.   runnum := maxrun;      { max number of tree cells to add to a branch }
  268.   connect(x, y);        { first connect frontier cell to the tree }
  269.   addfront(x, y);       { convert neighboring unused cells to frontier }
  270.   dec(runnum);         { number of tree cells left to add to this branch }
  271.   while (runnum > 0) and (fnum > 0) and (runset > 0) do
  272.   begin
  273.     repeat
  274.       d := randomdir;
  275.     until d and runset > 0;  { pick random direction to known frontier }
  276.     rembar(x, y, d);          {    and make it part of the tree }
  277.     i := adjust(x, y, d);
  278.     addfront(x, y);      { then pick up the neighboring frontier cells }
  279.     dec(runnum);
  280.   end;
  281. end;
  282.  
  283.  
  284. procedure drawmaze;
  285. var
  286.   x, y, i : word;
  287. begin
  288.   setcolor(gridcolor);    { draw the grid }
  289.   y := height * blockwidth;
  290.   for i := 0 to width do
  291.   begin
  292.     x := i * blockwidth;
  293.     line(x, 0, x, y);
  294.   end;
  295.   x := width * blockwidth;
  296.   for i := 0 to height do
  297.   begin
  298.     y := i * blockwidth;
  299.     line (0, y, x, y);
  300.   end;
  301.   fillchar(cell^, sizeof(cell^), chr(0));    { zero flags }
  302.   fnum   := 0;   { number of frontier cells in flist }
  303.   runset := 0; { directions to known frontier cells from a base cell }
  304.   randomize;
  305.   x := random(width) + 1;   { pick random start cell }
  306.   y := random(height) + 1;
  307.   add(x, y, rightdir);       { direction ignored }
  308.   addfront(x, y);      { start with 1 tree cell and some frontier cells }
  309.   while (fnum > 0) do
  310.   with flist[random(fnum) + 1] do
  311.     branch(column, row);
  312. end;
  313.  
  314. procedure dot(x, y, colr : word);
  315. begin
  316.   putpixel(blockwidth * x - halfblock, blockwidth * y - halfblock, colr);
  317. end;
  318.  
  319. procedure solve(x, y, endx, endy : word);
  320. var
  321.   j, k : word;
  322.   d    : byte;
  323.   i    : boolean;
  324. begin
  325.   d := rightdir;  { starting from left side of maze going right }
  326.   while (x <> endx) or (y <> endy) do
  327.   begin
  328.     if d = $01 then
  329.       d := $08
  330.     else
  331.       d := d shr 1; { look right, hug right wall }
  332.     while cell^[x,y].flags and d = 0 do
  333.     begin { look for an opening }
  334.       d := d shl 1;                            { if no opening, turn left }
  335.       if d > $08 then
  336.         d := d shr 4;
  337.     end;
  338.     j := x;
  339.     k := y;
  340.     i := adjust(x, y, d);         { go in that direction }
  341.     with cell^[j,k] do
  342.     begin    { turn on dot, off if we were here before }
  343.       flags := ((((cell^[x,y].flags xor $80) xor flags) and $80) xor flags);
  344.       if flags and $80 <> 0 then
  345.         dot(j, k, solvecolor)
  346.       else
  347.         dot(j, k, background);
  348.     end;
  349.   end;
  350.   dot(endx, endy, solvecolor);    { dot last cell on }
  351. end;
  352.  
  353. procedure mansolve (x,y,endx,endy: word);
  354. var
  355.   j, k : word;
  356.   d    : byte;
  357.   ch   : char;
  358. begin
  359.   ch := ' ';
  360.   while ((x <> endx) or (y <> endy)) and (ch <> 'X') and (ch <> #27) do
  361.   begin
  362.     dot(x, y, solvecolor);    { dot man on, show where we are in maze }
  363.     ch := upcase(readkey);
  364.     dot(x, y, background);    { dot man off after keypress }
  365.     d := 0;
  366.     case ch of
  367.       #0:
  368.       begin
  369.         ch := readkey;
  370.         case ch of
  371.           #72 : d := updir;
  372.           #75 : d := leftdir;
  373.           #77 : d := rightdir;
  374.           #80 : d := downdir;
  375.         end;
  376.       end;
  377.  
  378.       'I' : d := updir;
  379.       'J' : d := leftdir;
  380.       'K' : d := rightdir;
  381.       'M' : d := downdir;
  382.     end;
  383.  
  384.     if d > 0 then
  385.     begin
  386.       j := x;
  387.       k := y;    { move if no wall and still in bounds }
  388.       if (cell^[x,y].flags and d > 0) and adjust(j, k, d) then
  389.       begin
  390.         x := j;
  391.         y := k;
  392.       end;
  393.     end;
  394.   end;
  395. end;
  396.  
  397. procedure solvemaze;
  398. var
  399.   x, y,
  400.   endx,
  401.   endy : word;
  402.   ch   : char;
  403. begin
  404.   x := 1;                         { pick random start on left side wall }
  405.   y := random(height) + 1;
  406.   endx := width;                  { pick random end on right side wall }
  407.   endy := random(height) + 1;
  408.   remline(x, y, leftdir);         { show start and end by erasing line }
  409.   remline(endx, endy, rightdir);
  410.   mansolve(x, y, endx, endy);      { try it manually }
  411.   solve(x, y, endx, endy);         { show how when he gives up }
  412.   while keypressed do
  413.     ch := readkey;
  414.   ch := readkey;
  415. end;
  416.  
  417.  
  418. procedure getsize;
  419. var
  420.   j, k : real;
  421. begin
  422.   clrscr;
  423.   writeln('       Mind');
  424.   writeln('       Over');
  425.   writeln('       Maze');
  426.   writeln;
  427.   writeln('   by Randy Ding');
  428.   writeln;
  429.   writeln('Use I,J,K,M or arrow keys to walk thru maze,');
  430.   writeln('then hit X when you give up!');
  431.   repeat
  432.     writeln;
  433.     write('Maze size: ', minblockwidth, ' (hard) .. 95 (easy) ');
  434.     readln(blockwidth);
  435.   until (blockwidth >= minblockwidth) and (blockwidth < 96);
  436.   writeln;
  437.   write('Maximum branch length: 1 easy .. 50 harder, (0 unlimited) ');
  438.   readln(maxrun);
  439.   if maxrun <= 0 then
  440.     maxrun := 65535;  { infinite }
  441.   j := screenwidth / blockwidth;
  442.   k := screenheight / blockwidth;
  443.   if j = int(j) then
  444.     j := j - 1;
  445.   if k = int(k) then
  446.     k := k - 1;
  447.   width  := trunc(j);
  448.   height := trunc(k);
  449.   if (width > maxx) or (height > maxy) then
  450.   begin
  451.     width  := maxx;
  452.     height := maxy;
  453.   end;
  454.   halfblock := blockwidth div 2;
  455. end;
  456.  
  457. begin
  458.   repeat
  459.     getsize;
  460.     initbgi;
  461.     new(cell);    { allocate this large array on heap }
  462.     drawmaze;
  463.     solvemaze;
  464.     dispose(cell);
  465.     closegraph;
  466.     while keypressed do
  467.       ch := readkey;
  468.     write ('another one? ');
  469.     ch := upcase (readkey);
  470.   until (ch = 'N') or (ch = #27);
  471. end.
  472.  
  473.